package teste;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import tcc.caminhoneiro.R;
import entidade.caminhoneiro.Caminhoneiro;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_Menu extends ActionBarActivity implements OnItemClickListener{

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    
    private int notification_time;
    
    public static Integer user_id = null;
    
    public static boolean isNotifyOn = false;
    public static boolean isNotifyChargeOn = false;
    
    FragmentManager manager = getFragmentManager();
	FragmentTransaction transaction = manager.beginTransaction();
	
	ListView lvListView;
	String[] menu = {"Meu Perfil", "Meus Fretes", "Buscar Frete", "Sair"};
	Caminhoneiro caminhoneiro;
	
	String locationProvider = LocationManager.NETWORK_PROVIDER;
	
	boolean flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		inicializar();

		Location l = new Location("l");
		l.setLatitude(-14.7967910);
		l.setLongitude(-39.0402800);
	
		Location ll = new Location("ll");
		ll.setLatitude(-14.7966147);
		ll.setLongitude(-39.0379089);
		
		TextView lat = (TextView)findViewById(R.id.lat);
        lat.setText(String.valueOf( (int)l.distanceTo(ll) ));
		
		//getLocationFromAddress("1600 Amphitheatre Parkway,Mountain View,CA");
		//Local
		//startGPS();
		
		/*
		Local tLocal = new Local(getBaseContext());
		Location local = tLocal.getLocation();
		
		if (local!=null){
	    Toast.makeText(getBaseContext(), "Vai!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
		Toast.makeText(getBaseContext(), String.valueOf(local.getLatitude()), Toast.LENGTH_SHORT).show();
		Toast.makeText(getBaseContext(), String.valueOf(local.getLongitude()), Toast.LENGTH_SHORT).show();
		}
		*/
		
		/*
		Fragment_ResultActivityTeste fragment_result = new Fragment_ResultActivityTeste();
		
		this.transaction.replace(R.id.menu_principal_llFragment_teste, fragment_result);
		this.transaction.commit();
		*/
		
	}
	

	
	private void write(String file, String content){
		
		FileOutputStream fos;
		try {
			fos = openFileOutput(file, Context.MODE_PRIVATE);
			fos.write(content.getBytes());
			fos.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	private String readFile(String file){
		StringBuffer strBf = new StringBuffer("");
		String readString = "";
		try {
			FileInputStream fIn = openFileInput ( file ) ;
            InputStreamReader isr = new InputStreamReader ( fIn ) ;
            BufferedReader buffreader = new BufferedReader ( isr ) ;

            readString = buffreader.readLine();
            while ( readString != null ) {
                strBf.append(readString);
                readString = buffreader.readLine ( ) ;
            }
            buffreader.close();
            isr.close ( ) ;
            fIn.close();
            return strBf.toString();
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "time=99";
	}
	
	private void getLocationFromAddress(String strAddress){

		Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
		List<Address> address = new ArrayList<Address>();

		try {
			address = coder.getFromLocationName(strAddress, 5);
			TextView lat = (TextView)findViewById(R.id.lat);
			
			
			if (address == null || address.isEmpty()) {
				lat.setText("sem");
				return;
			}
			if (coder.isPresent()){
				lat.setText("presente");
			}
			
			Address location = address.get(0);
			lat.setText(String.valueOf(location.getLatitude()));
			//longitude = String.valueOf(location.getLongitude());

		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	
	public void startGPS(){
        LocationManager lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener lListener = new LocationListener() {
            public void onLocationChanged(Location locat) {
                updateView(locat);
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {
            	TextView lat = (TextView)findViewById(R.id.lat);
                lat.setText("status chang");
                
            }
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}

        };
        lManager.requestLocationUpdates(locationProvider, 0, 0, lListener);
        Location l = lManager.getLastKnownLocation(locationProvider);
        if ( lManager.isProviderEnabled(locationProvider)){
        	TextView lat = (TextView)findViewById(R.id.lat);
            lat.setText("iniOk");
            TextView lon = (TextView)findViewById(R.id.lon);
            if (l!=null){
            	lat.setText(String.valueOf(l.getLatitude()));
            	lon.setText(String.valueOf(l.getLongitude()));
            }
        }
    }
	
	public void updateView(Location locat){
        Double latitude = locat.getLatitude();
        Double longitude = locat.getLongitude();
        TextView lat = (TextView)findViewById(R.id.lat);
        TextView lon = (TextView)findViewById(R.id.lon);
        lat.setText(latitude.toString());
        lon.setText(longitude.toString());
        
		
    }
	
	public void inicializar(){
		lvListView 				= (ListView) findViewById(R.id.menu_principal_lvListView_teste);
        drawerLayout 			= (DrawerLayout) findViewById(R.id.drawer_layout_teste);
        actionBarDrawerToggle 	= new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close);
        
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.white)));
        getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        drawerLayout.setDrawerListener(actionBarDrawerToggle); 
		lvListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,menu));
        lvListView.setOnItemClickListener(this);
	}
	
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    	if(!flag){
         actionBarDrawerToggle.syncState();
    	}
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		

	}
}